//
//  XS_PlanChangeItemCell.swift
//  XueDao
//
//  Created by Mac on 2020/12/28.
//

import UIKit

protocol XS_PlanChangeItemCellDelegate {
    func changeItemCell(changeItemCell : XS_PlanChangeItemCell, didClickPlusBtn:UIButton)
    func changeItemCell(changeItemCell : XS_PlanChangeItemCell, didClickMinusBtn:UIButton)
}

class XS_PlanChangeItemCell: UICollectionViewCell {
    
    var delegate : XS_PlanChangeItemCellDelegate?

    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var countLabel: UILabel!
    
    var _model : XS_PlanItemModel!
    public var model : XS_PlanItemModel!{
        set{
            _model = newValue
            //设置数据
            imgView.sd_setImage(with: URL.init(string: newValue.curriculumImgUrl), completed: nil)
            titleLabel.text = newValue.curriculumName
            
            let startStr = "已学习 "
            let midStr = "\(newValue.learnedDay)"
            let endStr = " 天"
            let learnedDayAttStr = NSMutableAttributedString.init(string: startStr + midStr + endStr)
            learnedDayAttStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor(red: 0.16, green: 0.74, blue: 0.61, alpha: 1.0)], range: NSRange.init(location: startStr.count, length: midStr.count))
            
            timeLabel.attributedText = learnedDayAttStr
            contentLabel.text = Int(newValue.learnedDay)! > 0 ? ("学到 | " + (newValue.lastLearnPlace.count > 0 ? "\(newValue.lastLearnPlace)" : "\(newValue.curriculumName)")) : "学到 | 未开始学习"
            countLabel.text = String(newValue.planDay)
        }
        get{
            return _model
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @IBAction func plusBtnAction(_ sender: UIButton) {
        model.planDay = model.planDay + 1
        countLabel.text = String(model.planDay)
        if delegate != nil {
            delegate?.changeItemCell(changeItemCell: self, didClickPlusBtn: sender)
        }
    }
    
    @IBAction func minusBtnAction(_ sender: UIButton) {
        if model.planDay > 1 {
            model.planDay = model.planDay - 1
            countLabel.text = String(model.planDay)
            if delegate != nil {
                delegate?.changeItemCell(changeItemCell: self, didClickMinusBtn: sender)
            }
        }
        
    }
}
